package com.foxmail.aroundme.boring.manager;


import com.foxmail.aroundme.boring.BuildConfig;
import com.foxmail.aroundme.boring.bean.BaseResponse;
import com.foxmail.aroundme.boring.bean.newslist.NewsListBean;
import com.foxmail.aroundme.boring.callback.RequestCallback;
import com.foxmail.aroundme.boring.service.GifService;
import com.foxmail.aroundme.boring.service.JokeService;
import com.foxmail.aroundme.boring.service.NewsService;
import com.foxmail.aroundme.boring.service.WeiXinService;
import com.foxmail.aroundme.boring.utils.Config;
import com.foxmail.aroundme.boring.utils.Key;
import com.foxmail.aroundme.boring.utils.URL;
import com.foxmail.aroundme.boring.utils.m_enum.ChannelType;
import com.foxmail.aroundme.boring.utils.m_enum.RequestErrorType;
import com.foxmail.aroundme.boring.utils.net.RetrofitUtil;
import com.socks.library.KLog;

import java.util.List;

import retrofit2.Retrofit;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by gzl on 12/19/16.
 *
 * 获取网络的数据分发都在这里
 */

public class NetManager {

    private Retrofit retrofit;

    private static volatile NetManager netManager;

    //判断URL类型
    public static NetManager newsInstance() {
        if(netManager == null) {
            synchronized (NetManager.class) {
                if(netManager == null) {
                    netManager = new NetManager();
                }
            }
        }

        return netManager;
    }

    private NetManager() {}

    /**
     * 获取笑话接口，如果是获取最新的话后面两个参数没有
     *
     * @param callback CallBack
     * @param pageId 页数
     * @param pageSize 请求每页个数
     * @param sort 排序方式
     * @param time 时间
     */

    public void GetNewsFromNet(final RequestCallback<List<NewsListBean>> callback, final String channelId, int pageId, int pageSize,
                             @Config.SORT String sort, String time) {

        Observable<BaseResponse> observable = null;
        retrofit = RetrofitUtil.newsInstance().getRetrofit();
        switch (channelId) {
            //笑话接口
            case ChannelType.Joke:
                //获取最新的

                JokeService netService = retrofit.create(JokeService.class);

                if(sort == null || time == null) {
                    observable = netService.getNewJokeWithParam(Key.juhe_joke_key, pageId, pageSize);
                    if (BuildConfig.DEBUG) {
                       KLog.i("getNewJokeWithParam : id = " + pageId + " size = " + pageSize + " channel = 10001");
                    }
                } else {
                    //按时间获取
                    observable = netService.getJokeWithParam(Key.juhe_joke_key, pageId, pageSize, sort, time);
                    if (BuildConfig.DEBUG) {
                        KLog.i("getJokeWithParam : id = " + pageId + " size = " + pageSize + " channel = 10001");
                    }
                }
                break;
            //GIF接口
            case ChannelType.GIF:
                //获取最新的
                GifService gifService = retrofit.create(GifService.class);

                if(sort == null || time == null) {
                    observable = gifService.getNewGIFWithParam(Key.juhe_joke_key, pageId, pageSize);
                    if (BuildConfig.DEBUG) {
                        KLog.i("getNewGIFWithParam : id = " + pageId + " size = " + pageSize + " channel = 10002");
                    }
                } else {
                    //按时间获取
                    observable = gifService.getGIFWithParam(Key.juhe_joke_key, pageId, pageSize, sort, time);
                    if (BuildConfig.DEBUG) {
                        KLog.i("getGIFWithParam : id = " + pageId + " size = " + pageSize + " channel = 10002");
                    }
                }
                break;

            case ChannelType.WeiXin:
                WeiXinService weiXinService = retrofit.create(WeiXinService.class);
                observable = weiXinService.getWeiXinWithParam(URL.WeiXinURL,Key.WeiXing_Key, pageId, pageSize);
                if (BuildConfig.DEBUG) {
                    KLog.d("getWeiXin = " + channelId);
                }
                break;

            //除了那些特例其他都是新闻接口
            default:
                NewsService newsService = retrofit.create(NewsService.class);
                observable = newsService.getNewsWithParam(URL.NewsURL, Key.news_Key,
                        ChannelManager.getInstance().disPatchChannelType(channelId));
                if (BuildConfig.DEBUG) {
                    KLog.d(channelId);
                }

                break;
        }

        if (observable == null) {
            return;
        }

        observable.observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BaseResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if(BuildConfig.DEBUG) {
                            KLog.e("msg", e.toString());
                        }
                        callback.requestError(e.toString(), RequestErrorType.NET_ERROR);
                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse == null) {
                            callback.requestError("Response is null", RequestErrorType.REQUEST_NONE);
                            if(BuildConfig.DEBUG) {
                                KLog.e("Response is null");
                            }
                            return;
                        }

                        List<NewsListBean> listBeen = null;

                        //因为微信和其他数据的数据结构不同，需要单独拿出来
                        try {
                            switch (channelId) {
                                case ChannelType.WeiXin:
                                    listBeen = baseResponse.result.list;
                                    break;

                                default:
                                    listBeen = baseResponse.result.data;
                                    break;
                            }

                            callback.requestSuccess(listBeen, channelId, false);
                        }catch (NullPointerException e) {
                            callback.requestError("解析数据失败", RequestErrorType.REQUEST_ERROR);
                            if(BuildConfig.DEBUG) {
                                KLog.e("解析数据失败");
                            }
                        }
                    }
                });

    }

}
